﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Assignment_3.Classes;

namespace Assignment_3.Feature___Users
{
    public partial class RetrieveUser : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["username"] == null || Session["usertype"] == null)
            {
                Response.Redirect("~/Feature - Users/Login.aspx");
            }

            if (((string)Session["usertype"]) != "Administrator")
            {
                Response.Redirect("~/Other - Error Pages/NoPermission.aspx");
            }

            if (Request.QueryString["ID"] != null)
            {
                string ID = Request.QueryString["ID"];

                if (Request.QueryString["UPDATE"] == null)
                {
                    lblMessage.Style["display"] = "block";
                    lblMessage.Style["width"] = "95%";
                    lblMessage.Style["margin"] = "20px 3% 0 3%";
                    lblMessage.Style["margin-top"] = "20px";
                    lblMessage.Style["-webkit-border-radius"] = "5px";
                    lblMessage.Style["-moz-border-radius"] = "5px";
                    lblMessage.Style["border-radius"] = "5px";
                    lblMessage.Style["background"] = "#E2F6C5 url(../Resources/Images/icn_alert_success.png) no-repeat";
                    lblMessage.Style["background-position"] = "10px 10px";
                    lblMessage.Style["border"] = "1px solid #79C20D";
                    lblMessage.Style["color"] = "#32510F";
                    lblMessage.Style["padding"] = "10px 0";
                    lblMessage.Style["text-indent"] = "40px";
                    lblMessage.Style["font-size"] = "14px";
                    lblMessage.Text = "User successfully created";
                }
                else
                {
                    lblMessage.Style["display"] = "block";
                    lblMessage.Style["width"] = "95%";
                    lblMessage.Style["margin"] = "20px 3% 0 3%";
                    lblMessage.Style["margin-top"] = "20px";
                    lblMessage.Style["-webkit-border-radius"] = "5px";
                    lblMessage.Style["-moz-border-radius"] = "5px";
                    lblMessage.Style["border-radius"] = "5px";
                    lblMessage.Style["background"] = "#E2F6C5 url(../Resources/Images/icn_alert_success.png) no-repeat";
                    lblMessage.Style["background-position"] = "10px 10px";
                    lblMessage.Style["border"] = "1px solid #79C20D";
                    lblMessage.Style["color"] = "#32510F";
                    lblMessage.Style["padding"] = "10px 0";
                    lblMessage.Style["text-indent"] = "40px";
                    lblMessage.Style["font-size"] = "14px";
                    lblMessage.Text = "User successfully updated";
                }

                UserAccount user = DatabaseManager.GetUserAccountByUsername(ID);
                Staff s = DatabaseManager.GetStaffByUsername(ID);

                if (user != null && s != null)
                {
                    displayUsername.Text = user.Username.ToString();
                    displayPassword.Text = user.Password.ToString();
                    displayUserType.Text = user.UserType.ToString();

                    displayStaffID.Text = s.StaffID.ToString();
                    displayName.Text = s.Name.ToString();

                    DateTime date = s.DOB;
                    string dob = String.Format("{0:yyyy-MM-dd}", date);
                    displayDOB.Text = dob;

                    displayBankNo.Text = s.BankNo.ToString();
                    displayAddress.Text = s.Address.ToString();
                    displayTelNo.Text = s.TelNo.ToString();
                    displayDutyType.Text = s.DutyType.ToString();
                }

                else
                {
                    displayUsername.Text = "NIL";
                    displayPassword.Text = "NIL";
                    displayUserType.Text = "NIL";

                    displayStaffID.Text = "NIL";
                    displayName.Text = "NIL";
                    displayDOB.Text = "NIL";
                    displayBankNo.Text = "NIL";
                    displayAddress.Text = "NIL";
                    displayTelNo.Text = "NIL";
                    displayDutyType.Text = "NIL";
                }
            }

            else
            {
                displayUsername.Text = "NIL";
                displayPassword.Text = "NIL";
                displayUserType.Text = "NIL";

                displayStaffID.Text = "NIL";
                displayName.Text = "NIL";
                displayDOB.Text = "NIL";
                displayAddress.Text = "NIL";
                displayTelNo.Text = "NIL";
                displayDutyType.Text = "NIL";
            }
        }

        protected void searchBtn_Click(object sender, EventArgs e)
        {
            Page.Validate();
            if (Page.IsValid)
            {
                UserAccount user = null;
                Staff s = null;

                if (tbxSearch.Text != null)
                {
                    string username = tbxSearch.Text;

                    user = DatabaseManager.GetUserAccountByUsername(username);
                    s = DatabaseManager.GetStaffByUsername(username);

                    if (user != null && s != null)
                    {
                        lblMessage.Style["display"] = "block";
                        lblMessage.Style["width"] = "95%";
                        lblMessage.Style["margin"] = "20px 3% 0 3%";
                        lblMessage.Style["margin-top"] = "20px";
                        lblMessage.Style["-webkit-border-radius"] = "5px";
                        lblMessage.Style["-moz-border-radius"] = "5px";
                        lblMessage.Style["border-radius"] = "5px";
                        lblMessage.Style["background"] = "#E2F6C5 url(../Resources/Images/icn_alert_success.png) no-repeat";
                        lblMessage.Style["background-position"] = "10px 10px";
                        lblMessage.Style["border"] = "1px solid #79C20D";
                        lblMessage.Style["color"] = "#32510F";
                        lblMessage.Style["padding"] = "10px 0";
                        lblMessage.Style["text-indent"] = "40px";
                        lblMessage.Style["font-size"] = "14px";
                        lblMessage.Text = "User successfully retrieved...";

                        displayUsername.Text = user.Username.ToString();
                        displayPassword.Text = user.Password.ToString();
                        displayUserType.Text = user.UserType.ToString();

                        displayStaffID.Text = s.StaffID.ToString();
                        displayName.Text = s.Name.ToString();

                        DateTime date = s.DOB;
                        string dob = String.Format("{0:yyyy-MM-dd}", date);
                        displayDOB.Text = dob;

                        displayBankNo.Text = s.BankNo.ToString();
                        displayAddress.Text = s.Address.ToString();
                        displayTelNo.Text = s.TelNo.ToString();
                        displayDutyType.Text = s.DutyType.ToString();

                        if (user.UserType.Equals("Administrator"))
                        {
                            updateBtn.Visible = false;
                            deleteBtn.Visible = false;
                        }
                        else
                        {
                            updateBtn.Visible = true;
                            deleteBtn.Visible = true;
                        }

                    }

                    else
                    {
                        lblMessage.Style["display"] = "block";
                        lblMessage.Style["width"] = "95%";
                        lblMessage.Style["margin"] = "20px 3% 0 3%";
                        lblMessage.Style["margin-top"] = "20px";
                        lblMessage.Style["-webkit-border-radius"] = "5px";
                        lblMessage.Style["-moz-border-radius"] = "5px";
                        lblMessage.Style["border-radius"] = "5px";
                        lblMessage.Style["background"] = "#F3D9D9 url(../Resources/Images/icn_alert_error.png) no-repeat";
                        lblMessage.Style["background-position"] = "10px 10px";
                        lblMessage.Style["border"] = "1px solid #D20009";
                        lblMessage.Style["color"] = "#7B040F";
                        lblMessage.Style["padding"] = "10px 0";
                        lblMessage.Style["text-indent"] = "40px";
                        lblMessage.Style["font-size"] = "14px";
                        lblMessage.Text = "User does not exist...";

                        displayUsername.Text = "NIL";
                        displayPassword.Text = "NIL";
                        displayUserType.Text = "NIL";

                        displayStaffID.Text = "NIL";
                        displayName.Text = "NIL";
                        displayDOB.Text = "NIL";
                        displayBankNo.Text = "NIL";
                        displayAddress.Text = "NIL";
                        displayTelNo.Text = "NIL";
                        displayDutyType.Text = "NIL";

                        updateBtn.Visible = false;
                        deleteBtn.Visible = false;
                    }
                }

                else
                {
                    lblMessage.Style["display"] = "block";
                    lblMessage.Style["width"] = "95%";
                    lblMessage.Style["margin"] = "20px 3% 0 3%";
                    lblMessage.Style["margin-top"] = "20px";
                    lblMessage.Style["-webkit-border-radius"] = "5px";
                    lblMessage.Style["-moz-border-radius"] = "5px";
                    lblMessage.Style["border-radius"] = "5px";
                    lblMessage.Style["background"] = "#F3D9D9 url(../Resources/Images/icn_alert_error.png) no-repeat";
                    lblMessage.Style["background-position"] = "10px 10px";
                    lblMessage.Style["border"] = "1px solid #D20009";
                    lblMessage.Style["color"] = "#7B040F";
                    lblMessage.Style["padding"] = "10px 0";
                    lblMessage.Style["text-indent"] = "40px";
                    lblMessage.Style["font-size"] = "14px";
                    lblMessage.Text = "User unable to retrieve...";

                    displayUsername.Text = "NIL";
                    displayPassword.Text = "NIL";
                    displayUserType.Text = "NIL";

                    displayStaffID.Text = "NIL";
                    displayName.Text = "NIL";
                    displayDOB.Text = "NIL";
                    displayBankNo.Text = "NIL";
                    displayAddress.Text = "NIL";
                    displayTelNo.Text = "NIL";
                    displayDutyType.Text = "NIL";

                    updateBtn.Visible = false;
                    deleteBtn.Visible = false;
                }
            }
        }

        protected void updateBtn_Click(object sender, EventArgs e)
        {
            string ID = tbxSearch.Text;
            string query = "ID=" + ID;

            Response.Redirect("~/Feature - Users/UpdateUser.aspx?" + query);
        }

        protected void deleteBtn_Click(object sender, EventArgs e)
        {
            string ID = tbxSearch.Text;
            int rowsDeleted = 0;

            rowsDeleted = DatabaseManager.DeleteStaff(ID);

            if (rowsDeleted != 0)
            {
                rowsDeleted = 0;
                rowsDeleted = DatabaseManager.DeleteUserAccount(ID);

                if (rowsDeleted != 0)
                {
                    lblMessage.Style["display"] = "block";
                    lblMessage.Style["width"] = "95%";
                    lblMessage.Style["margin"] = "20px 3% 0 3%";
                    lblMessage.Style["margin-top"] = "20px";
                    lblMessage.Style["-webkit-border-radius"] = "5px";
                    lblMessage.Style["-moz-border-radius"] = "5px";
                    lblMessage.Style["border-radius"] = "5px";
                    lblMessage.Style["background"] = "#E2F6C5 url(../Resources/Images/icn_alert_success.png) no-repeat";
                    lblMessage.Style["background-position"] = "10px 10px";
                    lblMessage.Style["border"] = "1px solid #79C20D";
                    lblMessage.Style["color"] = "#32510F";
                    lblMessage.Style["padding"] = "10px 0";
                    lblMessage.Style["text-indent"] = "40px";
                    lblMessage.Style["font-size"] = "14px";
                    lblMessage.Text = "User successfully deleted...";

                    tbxSearch.Text = null;

                    displayUsername.Text = "NIL";
                    displayPassword.Text = "NIL";
                    displayUserType.Text = "NIL";

                    displayStaffID.Text = "NIL";
                    displayName.Text = "NIL";
                    displayDOB.Text = "NIL";
                    displayBankNo.Text = "NIL";
                    displayAddress.Text = "NIL";
                    displayTelNo.Text = "NIL";
                    displayDutyType.Text = "NIL";

                    updateBtn.Visible = false;
                    deleteBtn.Visible = false;
                }
            }
        }
    }
}